package Models;

import java.io.Serializable;

public class Equipment implements Serializable {

	private String name;
	private int ID;
	private String currentUserNetID;
	private long timeCheckedOut;
	private int checkOutDays;
	private boolean isCheckedOut;

	public Equipment(String name, int ID, int checkOutDays) {
		this.name = name;
		this.ID = ID;
		this.checkOutDays = checkOutDays;
		timeCheckedOut = System.currentTimeMillis();
		isCheckedOut = false;
	}
	
	public Equipment(){
		name = "DVD";
		ID = 1;
		checkOutDays = 2;
		timeCheckedOut = System.currentTimeMillis() - (4*86400000);
		isCheckedOut = true;
	}

	public boolean isCheckedOut() {
		return isCheckedOut;
	}

	public void checkOut(String netID) throws CheckedOutException {
		if (isCheckedOut == false) {
			isCheckedOut = true;
			currentUserNetID = netID;
			DBSupport db = DBSupport.getDBSupportInstance();
			User u = db.getUser(netID.toLowerCase());
			u.addToHistory(ID);
		} else {
			throw new CheckedOutException();
		}
	}

	public void checkIn() {
		isCheckedOut = false;
		DBSupport.getDBSupportInstance().getUser(currentUserNetID)
				.addToHistory(ID);
		currentUserNetID = "";
	}

	public String getName() {
		return name;
	}

	public int getID() {
		return ID;
	}

	public String getCurrentUserNetID() {
		return currentUserNetID;
	}

	public boolean isOverdue() {
		if (isCheckedOut == false)
			return false;
		long elapsedTime = System.currentTimeMillis() - timeCheckedOut;
		int daysCheckedOut = (int) (elapsedTime / 86400000);
		if (daysCheckedOut > checkOutDays)
			return true;
		return false;
	}

	public void extendCheckout() {
		if (isCheckedOut == true) {
			timeCheckedOut = System.currentTimeMillis();
		}
	}

	public long getTimeCheckedOut() {
		return timeCheckedOut;
	}

	public int getCheckOutDays(){
		return checkOutDays;
	}
}
